unit dlg_aspellcfg;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, amgModulesShare, AspellHeadersDyn, Buttons, ShellAPI, MyAspell,
  ComCtrls, ExtCtrls;

type
  Tdlg_aspellconfig = class(TForm)
    GroupBox2: TGroupBox;
    cbConfig: TComboBox;
    lblHint: TLabel;
    memText: TMemo;
    edtLine: TEdit;
    cbBool: TCheckBox;
    bbtnSave: TBitBtn;
    StatusBar: TStatusBar;
    bbtnDefault: TBitBtn;
    Image1: TImage;
    lblInfo: TLabel;
    procedure cbConfigChange(Sender: TObject);
    procedure bbtnSaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure bbtnDefaultClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Spell:TASpell;
    procedure load_cfg();
    procedure load_opt();
    function get_type(t:AspellKeyInfoType):string;
  end;

var
  dlg_aspellconfig: Tdlg_aspellconfig;

implementation

uses dlg_trans;

{$R *.dfm}


procedure load_aspell_config_file(const fn:string);
begin

end;


{ Tdlg_aspellconfig }


procedure Tdlg_aspellconfig.load_cfg;
begin
  spell.GetConfigKeys(cbConfig.Items);
end;

procedure Tdlg_aspellconfig.load_opt;
var
  info:AspellKeyInfo;
begin
  lblHint.Caption  := '';
  lblInfo.Caption  := '';
  memText.Visible  := false;
  cbBool.Visible   := false;
  edtLine.Visible  := false;

  info := spell.GetConfigKeyInfo(cbConfig.Text);

  if info <> nil then
    begin
      lblHint.Caption := info.desc;
      lblInfo.Caption := get_type(info.type_) + ' (' + info.def +')';
      case info.type_ of
        AspellKeyInfoString: begin
                               edtLine.Visible := true;
                               edtLine.Text := spell.GetConfigValue(info.name);
                             end;
        AspellKeyInfoInt:    begin
                               edtLine.Visible  := true;
                               edtLine.Text := spell.GetConfigValue(info.name); //inttostr(aspell_config_retrieve_int( cnf, info.name ));
                             end;
        AspellKeyInfoBool:   begin
                               cbBool.Visible := true;
                               cbBool.Caption := cbConfig.Text;
                               cbBool.Checked := spell.GetConfigValueBool(info.name);
                             end;
        AspellKeyInfoList:   begin
                               memText.Visible := true;
                               spell.GetConfigValueList(info.name, memText.Lines);
                             end;
      end;
    end;
end;

procedure Tdlg_aspellconfig.cbConfigChange(Sender: TObject);
begin
  load_opt();
end;

procedure Tdlg_aspellconfig.bbtnSaveClick(Sender: TObject);
var
  info:AspellKeyInfo;

  function TryText():boolean;
  begin
    Result := true;
    try
      StrToInt(edtLine.Text);
    except
      Result := false;
    end
  end;

begin
  if cbConfig.Text = '' then
    exit;
  info := spell.GetConfigKeyInfo(cbConfig.Text);
  if info <> nil then
    begin
      case info.type_ of
        AspellKeyInfoString: begin
                               spell.SetConfigValue(info.Name, edtLine.Text);
                             end;
        AspellKeyInfoInt   : begin
                               if TryText() then
                                 spell.SetConfigValueInt(info.Name, StrToInt(edtLine.Text))
                               else
                                 MessageDlg('Wartosc musi byc liczba!', mtError, [mbOk], 0);
                             end;
        AspellKeyInfoBool:   begin
                               spell.SetConfigValueBool(info.Name, cbBool.Checked);
                             end;
        AspellKeyInfoList:   begin
                               spell.SetConfigValueList(info.Name, memText.Lines);
                             end;
      end;
    end;
  StatusBar.SimpleText := spell.Error;
end;

procedure Tdlg_aspellconfig.FormShow(Sender: TObject);
begin
  load_cfg();
  cbConfig.ItemIndex := 0;
end;

function Tdlg_aspellconfig.get_type(t: AspellKeyInfoType): string;
begin
    result := '';
    case t of
      AspellKeyInfoString: result := 'tekst';
      AspellKeyInfoInt:    result := 'numer';
      AspellKeyInfoBool:   result := 'bool';
      AspellKeyInfoList:   result := 'lista';
    end;
end;

procedure Tdlg_aspellconfig.bbtnDefaultClick(Sender: TObject);
var
  info:AspellKeyInfo;

  function TryText():boolean;
  begin
    Result := true;
    try
      StrToInt(edtLine.Text);
    except
      Result := false;
    end
  end;

begin
  if cbConfig.Text = '' then
    exit;
  info := spell.GetConfigKeyInfo(cbConfig.Text);
  if info <> nil then
      case info.type_ of
        AspellKeyInfoString,
        AspellKeyInfoInt:   edtLine.Text := info.def;
        AspellKeyInfoBool:  cbBool.Checked := info.def = 'true';
        AspellKeyInfoList:  memText.Lines.Text := info.def;
      end;
end;

end.
